package com.weetop.aklyscan.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.UserInfoBean;
import com.weetop.aklyscan.presenter.SendOutPresent;
import com.weetop.aklyscan.view.SendOutView;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class PrinterActivity extends BaseActivity<SendOutPresent> implements SendOutView {
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.ip)
    EditText ip;
    @BindView(R.id.sn)
    TextView sn;
    @BindView(R.id.zzkd)
    EditText zzkd;
    @BindView(R.id.sppy)
    EditText sppy;
    @BindView(R.id.czpy)
    EditText czpy;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                mPresenter.Into44(name.getText().toString().trim(), ip.getText().toString().trim(), sn.getText().toString().trim(), "",
                        zzkd.getText().toString().trim(), "", sppy.getText().toString().trim(), czpy.getText().toString().trim());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer);
        ButterKnife.bind(this);
        mPresenter.Into45();
    }

    @Override
    protected SendOutPresent createPresenter() {
        return new SendOutPresent(this, this);
    }

    @OnClick({R.id.iv_back, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.button:
                if (TextUtils.isEmpty(name.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(PrinterActivity.this, "打印设备不能为空");
                    return;
                }
                if (TextUtils.isEmpty(ip.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(PrinterActivity.this, "IP不能为空");
                    return;
                }
                if (TextUtils.isEmpty(zzkd.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(PrinterActivity.this, "纸张宽度不能为空");
                    return;
                }
                if (TextUtils.isEmpty(sppy.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(PrinterActivity.this, "水平偏移不能为空");
                    return;
                }
                if (TextUtils.isEmpty(czpy.getText().toString().trim())) {
                    ToastUtil.getInstance()._short(PrinterActivity.this, "垂直偏移不能为空");
                    return;
                }
                getDaYin();
                break;
        }
    }

    @Override
    public void getInto44Success(BaseModel<String> o) {
        ToastUtil.getInstance()._short(this, "保存成功");
        mPresenter.Into45();
    }

    @Override
    public void getInto45Success(BaseModel<UserInfoBean> o) {
        MainActivity.userInfoBean = o.getData();
        name.setText(MainActivity.userInfoBean.getPrint_equipment());
        ip.setText(MainActivity.userInfoBean.getPrint_ip());
        sn.setText(MainActivity.userInfoBean.getPrint_ssid());
        zzkd.setText(MainActivity.userInfoBean.getPrint_paper_width());
        sppy.setText(MainActivity.userInfoBean.getPrint_deviation_x());
        czpy.setText(MainActivity.userInfoBean.getPrint_deviation_y());
    }

    private void getDaYin() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
//                    int i = MainActivity.mZPL.PortOpen("WiFi,192.168.43.114,9100");
                    int i = MainActivity.mZPL.PortOpen("WiFi," + ip.getText().toString().trim() + ",9100");
                    if (i == 0) {
                        ToastUtil.getInstance()._short(mContext, "连接成功");
                        handler.sendEmptyMessage(1);
                    } else {
                        ToastUtil.getInstance()._short(mContext, "连接失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
